import 'package:sinosheets/utils/language/language_util.dart';
import 'package:sp_util/sp_util.dart';

class LSUtil {
  static Future<bool> init() async {
    await SpUtil.getInstance();
    return SpUtil.isInitialized();
  }

  ///用户信息的存储
  static UserInfo userInfo = UserInfo();

  ///Firebase订阅信息的存储
  static TopicInfo topicInfo = TopicInfo();

  ///保存和获取App的环境
  static void saveAppEnv(String env) {
    SpUtil.putString('app_env', env);
  }

  static String getAppEnv() {
    //todo: default value is prod
    //todo: default value is test
    return SpUtil.getString('app_env', defValue: 'prod') ?? 'prod';
  }

  /// 保存和获取登录状态
  static void saveLoginStatus(bool status) {
    SpUtil.putBool('login_status', status);
  }

  static bool getLoginStatus() {
    return SpUtil.getBool('login_status', defValue: false) ?? false;
  }

  ///保存和获取FCMToken
  static void saveFCMToken(String token) {
    if (token.isEmpty) {
      SpUtil.remove('fcm_token');
    } else {
      SpUtil.putString('fcm_token', token);
    }
  }

  static String getFCMToken() {
    return SpUtil.getString('fcm_token') ?? '';
  }

  ///保存和获取用户是否开启推送
  static void savePushStatus(bool status) {
    SpUtil.putBool('push_status', status);
  }

  static bool getPushStatus() {
    return SpUtil.getBool('push_status', defValue: false) ?? false;
  }

  ///保存和获取邀请码 code和是否有效两个字段
  static void saveInviteCode(String code, bool isValid) {
    if (code.isEmpty) {
      SpUtil.remove('invite_code');
      SpUtil.remove('invite_code_valid');
    } else {
      SpUtil.putString('invite_code', code);
      SpUtil.putBool('invite_code_valid', isValid);
    }
  }

  static Map getInviteCode() {
    // return TurbolinkUtils.checkCode(SpUtil.getString('invite_code') ?? '');
    return {
      'code': SpUtil.getString('invite_code') ?? '',
      'isValid': SpUtil.getBool('invite_code_valid', defValue: false) ?? false,
    };
  }

  ///保存和获取是否显示Guide
  static void saveGuideStatus(bool status) {
    SpUtil.putBool('guide_status', status);
  }

  static bool getGuideStatus() {
    return SpUtil.getBool('guide_status', defValue: true) ?? true;
  }

  ///保存和获取商品词搜关键字列表
  static void saveGoodSearchKeywords(List<String> keywords) {
    if (keywords.isEmpty) {
      SpUtil.remove('good_search_keywords');
    } else {
      SpUtil.putStringList('good_search_keywords', keywords);
    }
  }

  static List<String> getGoodSearchKeywords() {
    return SpUtil.getStringList('good_search_keywords') ?? [];
  }

  ///保存和获取店铺词搜关键字列表
  static void saveShopSearchKeywords(List<String> keywords) {
    if (keywords.isEmpty) {
      SpUtil.remove('shop_search_keywords');
    } else {
      SpUtil.putStringList('shop_search_keywords', keywords);
    }
  }

  static List<String> getShopSearchKeywords() {
    return SpUtil.getStringList('shop_search_keywords') ?? [];
  }

  ///保存和获取H5页面的版本号
  static void saveH5Version(String version) {
    SpUtil.putString('h5_version', version);
  }

  static String getH5Version() {
    return SpUtil.getString('h5_version') ?? '';
  }

  /// 保存和获取用户多语言
  static void saveLanguage(String lang) {
    if (LanguageUtil.supportLanguageCodeList.contains(lang)) {
      SpUtil.putString('language', lang);
    }
  }

  static String getLanguage() {
    return SpUtil.getString('language', defValue: 'en') ?? 'en';
  }

  ///保存和获取app版本号
  static void saveAPPVersion(String version) {
    SpUtil.putString('app_version', version);
  }

  static String getAPPVersion() {
    return SpUtil.getString('app_version') ?? '';
  }

  /// 保存和获取用户请求Token
  static void saveToken(String token) {
    if (token.isEmpty) {
      SpUtil.remove('token');
    } else {
      SpUtil.putString('token', token);
    }
  }

  static String getToken() {
    return SpUtil.getString('token') ?? '';
  }

  static void saveTokenHead(String tokenHead) {
    if (tokenHead.isEmpty) {
      SpUtil.remove('token_head');
    } else {
      SpUtil.putString('token_head', tokenHead);
    }
  }

  static void saveRefreshToken(String refreshToken) {
    if (refreshToken.isEmpty) {
      SpUtil.remove('refresh_token');
    } else {
      SpUtil.putString('refresh_token', refreshToken);
    }
  }

  static String getTokenAll() {
    String head = SpUtil.getString('token_head') ?? '';
    String token = SpUtil.getString('token') ?? '';
    return '$head $token';
  }

  ///保存和获取设备码
  static void saveDeviceCode(String code) {
    SpUtil.putString('device_code', code);
  }

  static String getDeviceCode() {
    return SpUtil.getString('device_code') ?? '';
  }

  /// 保存和获取主题
  static void saveThemeCode(String code) {
    if (code.isEmpty) {
      SpUtil.putString('theme_code', "light");
    } else {
      SpUtil.putString('theme_code', code);
    }
  }

  static String getThemeCode() {
    return SpUtil.getString('theme_code', defValue: "light") ?? "light";
  }

  ///保存和获取剪切板的url
  static void saveClipboardUrl(String url) {
    SpUtil.putString('clipboard_url', url);
  }

  static String getClipboardUrl() {
    return SpUtil.getString('clipboard_url') ?? '';
  }

  ///保存和获取剪切板的状态
  static void saveClipboardStatus(bool status) {
    SpUtil.putBool('clipboard_status', status);
  }

  static bool getClipboardStatus() {
    return SpUtil.getBool('clipboard_status', defValue: false) ?? false;
  }

  /// 保存和获取国家
  static void saveSelectCountry(Map country) {
    SpUtil.putObject('select_country', country);
  }

  static Map getSelectCountry() {
    return SpUtil.getObject('select_country') ?? {};
  }

  ///保存和获取设备的底部安全距离
  static void saveBottomSafeHeight(double height) {
    SpUtil.putDouble('bottom_safe_height', height);
  }

  static double getBottomSafeHeight() {
    return SpUtil.getDouble('bottom_safe_height', defValue: 0) ?? 0;
  }

  /// 保存和获取币种信息
  static void saveCurrency(Map currency) {
    if (currency.isEmpty) {
      SpUtil.remove("currency");
    } else {
      SpUtil.putObject("currency", currency);
    }
  }

  static Map getCurrency() {
    return SpUtil.getObject('currency') ?? {};
  }

  ///保存和获取H5保存的信息
  static void saveH5Local(Map local) {
    if (local.isEmpty) {
      SpUtil.remove('h5_local');
    } else {
      SpUtil.putObject('h5_local', local);
    }
  }

  static Map getH5Local() {
    return SpUtil.getObject('h5_local') ?? {};
  }

  ///保存和获取广告展示日期
  static void saveAdShowDate(String date) {
    SpUtil.putString('ad_show_date', date);
  }

  static String getAdShowDate() {
    return SpUtil.getString('ad_show_date') ?? '';
  }

  ///保存和获取广告展示日期和id的map
  static void saveAdShowDateAndId(Map<String, dynamic> map) {
    Map oldData = SpUtil.getObject('ad_show_date_and_id') ?? {};
    oldData.addAll(map);
    SpUtil.putObject('ad_show_date_and_id', oldData);
  }

  static Map getAdShowDateAndId() {
    return SpUtil.getObject('ad_show_date_and_id') ?? {};
  }

  ///保存sku中的免责声明同意与否
  static void saveSkuDisclaimer(bool isAgree) {
    SpUtil.putBool('sku_disclaimer', isAgree);
  }

  static bool getSkuDisclaimer() {
    return SpUtil.getBool('sku_disclaimer', defValue: false) ?? false;
  }

  /// 保存和获取预演提示弹窗的状态
  static void savePreviewAlertStatus(bool status) {
    SpUtil.putBool('preview_alert_status_key', status);
  }

  static bool getPreviewAlertStatus() {
    return SpUtil.getBool('preview_alert_status_key', defValue: false) ?? false;
  }

  ///保存和获取去包装提示弹框的状态
  static void saveRemovePackInfoStatus(bool status) {
    SpUtil.putBool('remove_pack_info_status_key', status);
  }

  static bool getRemovePackInfoStatus() {
    return SpUtil.getBool('remove_pack_info_status_key', defValue: false) ?? false;
  }

  ///保存24小时倒计时是否显示过
  static void saveFreeActivityTimeLessThan24HoursStatus(bool status) {
    SpUtil.putBool('free_activity_time_less_than_24_hours_status_key', status);
  }

  static bool getFreeActivityTimeLessThan24HoursStatus() {
    return SpUtil.getBool('free_activity_time_less_than_24_hours_status_key', defValue: false) ?? false;
  }

  ///保存和获取系统开关
  static void saveSheetShowCode(String code) {
    SpUtil.putString('sheet_show_code', code);
  }

  static String getSheetShowCode() {
    return SpUtil.getString('sheet_show_code') ?? 'false';
  }
}

class UserInfo {
  /// 保存用户信息
  void saveUserInfo(Map userInfo) {
    if (userInfo.isEmpty) {
      SpUtil.remove('user_info');
    } else {
      SpUtil.putObject('user_info', userInfo);
      SpUtil.putString('old_account', userInfo['account']);
    }
  }

  //获取用户邮箱
  String getUserEmail() {
    return SpUtil.getObject('user_info')?['email'] ?? '';
  }

  //获取用户账号
  String getUserAccount() {
    return SpUtil.getObject('user_info')?['account'] ?? '';
  }

  //获取用户名称
  String getUserName() {
    return SpUtil.getObject('user_info')?['userName'] ?? '';
  }

  //获取用户头像
  String getUserAvatar() {
    return SpUtil.getObject('user_info')?['avatar'] ?? '';
  }

  //获取用户手机号
  String getUserPhone() {
    return SpUtil.getObject('user_info')?['phone'] ?? '';
  }

  // 获取用户ID
  String getUserId() {
    return "${SpUtil.getObject('user_info')?['userId'] ?? ''}";
  }

  //获取用户等级
  String getUserLevel() {
    return '${SpUtil.getObject('user_info')?['level'] ?? 0}';
  }

  //获取用户成长值
  String getUserGrowth() {
    return '${SpUtil.getObject('user_info')?['growth'] ?? 0}';
  }

  //获取用户仓库id
  String getStorageId() {
    return '${SpUtil.getObject('user_info')?['storageId'] ?? 0}';
  }

  //获取用户上次登录账号
  String getOldAccount() {
    return SpUtil.getString('old_account') ?? '';
  }

  //获取用户国家Code
  String getUserCountryCode() {
    return SpUtil.getObject('user_info')?['countryCode'] ?? '';
  }

  //获取用户语言
  String getUserLanguage() {
    return SpUtil.getObject('user_info')?['language'] ?? '';
  }
}

class TopicInfo {
  ///保存和获取随机Topic
  void saveRandomTopic(String topic) {
    SpUtil.putString('random_topic', topic);
  }

  String getRandomTopic() {
    return SpUtil.getString('random_topic') ?? '';
  }

  ///保存和获取国家Topic
  void saveCountryTopic(String topic) {
    SpUtil.putString('country_topic', topic);
  }

  String getCountryTopic() {
    return SpUtil.getString('country_topic') ?? '';
  }

  ///保存和获取国家+uid的Topic
  void saveCountryUidTopic(String topic) {
    SpUtil.putString('country_uid_topic', topic);
  }

  String getCountryUidTopic() {
    return SpUtil.getString('country_uid_topic') ?? '';
  }

  //保存和获取uid的Topic
  void saveUidTopic(String topic) {
    SpUtil.putString('uid_topic', topic);
  }

  String getUidTopic() {
    return SpUtil.getString('uid_topic') ?? '';
  }
}
